//
//  SCNNode+ShapeChange.swift
//  ARDEMO_1
//
//  Created by 37Design on 2021/5/27.
//

import Foundation
import SceneKit

extension SCNNode{
   
    /** 加宽 (上下前后改变scale,左右面改变position)*/
    func shapeModelWidthWith(currentNode: SCNNode, scale: Float){
        SCNTransaction.begin()
        SCNTransaction.animationDuration = 0.5
        for childNode in currentNode.childNodes {
            //根据各子节点名字得到板块对象
            if childNode.name == "Top" || childNode.name == "Bottom" || childNode.name == "Front" || childNode.name == "Back"{
                print(childNode.scale.x)
                childNode.scale.x = scale
//                childNode.enumerateChildNodes { topNode, Unsa in
//                    //放宽 - 需大于1
//                    topNode.scale.x = scale
//                }
            }else if(childNode.name == "Right"){
//           1.0 = -0.5  1.1 = 1.5  1.5 = 9.5   1.6 = 11.5  1.7 = 13.5
                let positionX = -0.5 + (scale - 1) * 10 * 2
                print(positionX)
                childNode.position.x = positionX
//                childNode.enumerateChildNodes { rightNode, Unsa in
//                    //放宽 - 需大于1
//                }
            }else if(childNode.name == "Left"){
                let positionX = -0.5 + (scale - 1) * 10 * 2
//              1.0 = -0.5  1.1 = 2.5   1.5 = 10.5   1.6 = 12.5  1.7 = 14.5  ----- 2.0 = 20.5
                print(positionX)
                childNode.position.x = -(positionX + 1)
//                childNode.enumerateChildNodes { leftNode, Unsa in
//                    //放宽 - 需大于1
//                }
            }
        }
        SCNTransaction.commit()
    }

    /**
     改变高度 （前后左右改变scale，上下改变position）
     */
    func shapemodelHeightWith(currentNode: SCNNode, scale: Float){
        SCNTransaction.begin()
        SCNTransaction.animationDuration = 0.5
        for childNode in currentNode.childNodes {
            //根据各子节点名字得到板块对象
            if childNode.name == "Left" || childNode.name == "Right" || childNode.name == "Front" || childNode.name == "Back"{
                childNode.scale.y = scale
            }else if(childNode.name == "Top"){
                let positionY = 0.289 + (scale - 1) * 10 * 1.25
                //放大0.1倍 + 1.25
                // 1.0 = 0.289 1.1 = 1.539  1.2 = 2.789   1.3 = 4.039  1.4 = 5.289  1.5 = 6.539
                childNode.position.y = positionY
            }else if(childNode.name == "Bottom"){
                // 1.0 = 0.289 1.1 = -0.939  1.2 = -2.189 1.3 = -3.439  1.4 = -4.689  1.5 = -5.989
                let positionY = 0.289 + (scale - 1) * 10 * 1.25
                childNode.position.y = -(positionY - 0.6)
            }
        }
        SCNTransaction.commit()
    }
    //根据第一个面的scale.x scale.y值自动布局
    func shapeModelEditerWith(currentNode: SCNNode){
        SCNTransaction.begin()
        SCNTransaction.animationDuration = 0.5
        let frontNode = findFrontScale(rootNode: currentNode)
        
        for childNode in currentNode.childNodes {
           
            if childNode.name == "Edit1" || childNode.name == "Edit2" || childNode.name == "Edit3"{
                print(frontNode.scale.x, frontNode.scale.y)
                if frontNode.scale.x > 1 {
                    print(childNode.position.x, childNode.position.y)
                    childNode.position.x *= frontNode.scale.x
                }else{
                    childNode.position.x *= frontNode.scale.x
                }
                if frontNode.scale.y > 1{
                    childNode.position.y *= frontNode.scale.y
                }
            }
        }
        SCNTransaction.commit()
    }
    
    func findFrontScale(rootNode: SCNNode) -> SCNNode {
        var node = SCNNode()
        for childNode in rootNode.childNodes {
            guard !(childNode.name == "Front") else {
                node = childNode
                return node
            }
        }
        print("没找到！")
        return node
    }
    
    /** 还原*/
    func reduction(currentNode: SCNNode){
        SCNTransaction.begin()
        SCNTransaction.animationDuration = 0.5
        for childNode in currentNode.childNodes {
            if childNode.name == "Left" || childNode.name == "Right" || childNode.name == "Front" || childNode.name == "Back" || childNode.name == "Top" || childNode.name == "Bottom" {
                childNode.scale.x = 1
                childNode.scale.y = 1
                childNode.scale.z = 1
                childNode.position.x = -0.501
                childNode.position.y = 0.289
                childNode.position.z = -5.025
                childNode.geometry?.firstMaterial?.diffuse.contents = UIColor.white
            }
        }
        SCNTransaction.commit()
    }
    
    
    
    func animationAction(){
        SCNTransaction.begin()
        SCNTransaction.animationDuration = 0.5

        // on completion - unhighlight
        SCNTransaction.completionBlock = {
            SCNTransaction.begin()
            SCNTransaction.animationDuration = 0.5
            SCNTransaction.commit()
        }
        SCNTransaction.commit()
    }
}
